struct TreeNode {
	int val;
	struct TreeNode *left;
    struct TreeNode *right;
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
#include <vector>

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @param sum int整型 
     * @return bool布尔型
     */
    void dfs(TreeNode*root,int sum,std::vector<int>&res,std::vector<int>&path)
    {
        if (!root)
        {
            return ;
        }   

        path.push_back(root->val);
        sum-=root->val;

        if (!root->left&&!root->right&&sum == 0)
        {
            res = path;
            return ;
        }

        dfs(root->left,sum,res, path);
        dfs(root->right,sum,res,path);
        
        path.pop_back();
    }
    bool hasPathSum(TreeNode* root, int sum) 
    {
        std::vector<int>res;
        std::vector<int>path;
        dfs(root,sum,res,path);

        if (res.size() == path.size())
        {
            return true;
        }
        else
        {
            return false;
        } 
    }
};

